package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeListQuery;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;
@Slf4j
@RestController
@Api(tags = "01.通知管理管理")
@RequestMapping("/v1/notice")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @ApiOperation("新增公告")
    @ApiOperationSupport(order = 10)
    @PostMapping("add")

    public JsonResult addNotice(NoticeAddParam noticeAddParam){
        log.debug("noticeAddParam"+noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        noticeMapper.insertNotice(notice);
        return JsonResult.ok();
    }
    @ApiOperationSupport(order = 20)
    @ApiOperation("查询公告列表")
    @GetMapping("list")
    public JsonResult noticeList(@ApiIgnore NoticeListQuery noticeListQuery){
        log.debug("noticeListQuery"+noticeListQuery);
        String title = noticeListQuery.getTitle();
        Integer type = noticeListQuery.getType();
       List<NoticeListVO> noticeListVOS= noticeMapper.selectNotice(title,type);
       return  JsonResult.ok(noticeListVOS);
    }
    @ApiOperationSupport(order = 30)
    @ApiOperation("查询公告详情")
    @GetMapping("detail")
    public JsonResult detail(Long id){
        log.debug("id = "+id);
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.detail(id);
        return  JsonResult.ok(noticeDetailInfoVO);
    }
    @ApiOperationSupport(order = 40)
    @ApiOperation("删除公告")
    @ApiImplicitParams(value = {    @ApiImplicitParam(name = "id",value = "通知编号",required = true,dataType = "Long"),
            @ApiImplicitParam(name="title",value = "通知标题",required = true,dataType = "String")})
    @PostMapping("delete")
    public JsonResult delNotice(Long id){
        log.debug("id = "+id);
        noticeMapper.delNotice(id);
        return  JsonResult.ok();
    }
    @ApiOperationSupport(order = 50)
    @ApiOperation("修改公告")
    @PostMapping("update")
    public JsonResult updateNotice(NoticeAddParam noticeAddParam){
        log.debug("noticeAddParam"+noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setUpdateTime(new Date());
        noticeMapper.updateNotice(notice);
        return  JsonResult.ok();
    }
}
